package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.s1995.sloth.bizs.entity.Prod;
import cn.s1995.sloth.bizs.entity.ProdGroup;
import cn.s1995.sloth.bizs.entity.ProdGroupReference;
import cn.s1995.sloth.bizs.oo.vo.ProdGroupReferenceVO;
import cn.s1995.sloth.bizs.service.ProdGroupReferenceService;
import cn.s1995.sloth.bizs.service.ProdGroupService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品分组引用
 *
 * @author sloth
 * @date 2023-12-20 09:52:18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/prodGroupReference")
@Tag(description = "ProdGroupReference", name = "BMS-产品分组引用管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProdGroupReferenceController {

    private final ProdGroupReferenceService prodGroupReferenceService;

    private final ProdGroupService prodGroupService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 产品分组引用
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject ProdGroupReference req) {
		MPJLambdaWrapper<ProdGroupReference> wrapper = new MPJLambdaWrapper<ProdGroupReference>()
	            .selectAll(ProdGroupReference.class)
	            .selectAssociation(Prod.class, ProdGroupReferenceVO::getProd)
//				.selectAs(Prod::getName, ProdGroupReferenceVO::getProdName)
//				.selectAs(Prod::getAmount, ProdGroupReferenceVO::getProdAmount)
//				.selectAs(Prod::getThumbnail, ProdGroupReferenceVO::getProdThumbnail)
//				.selectAs(Prod::getIsSale, ProdGroupReferenceVO::getProdIsSale)
				.leftJoin(Prod.class, Prod::getId, ProdGroupReference::getProdId)
				.eq(ObjectUtil.isNotNull(req.getGroupId()), ProdGroupReference::getGroupId, req.getGroupId());
        return R.ok(prodGroupReferenceService.selectJoinPage(page, wrapper));
    }

    /**
     * 获取列表
     * @param req 产品分组引用
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_view')")
    public R getList(@ParameterObject ProdGroupReference req) {
        LambdaQueryWrapper<ProdGroupReference> wrapper = Wrappers.lambdaQuery();
        return R.ok(prodGroupReferenceService.list(wrapper));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(prodGroupReferenceService.getById(id));
    }

    /**
     * 新增产品分组引用
     * @param req 产品分组引用
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增产品分组引用", description = "新增产品分组引用")
    @SysLog("新增产品分组引用")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_add')")
    public R save(@RequestBody ProdGroupReference req) {
		boolean res = prodGroupReferenceService.save(req);
		prodGroupService.updateProdCount(req.getGroupId());
        return R.ok(res);
    }

    /**
     * 修改产品分组引用
     * @param req 产品分组引用
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改产品分组引用", description = "修改产品分组引用")
    @SysLog("修改产品分组引用")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_edit')")
    public R updateById(@RequestBody ProdGroupReference req) {
        return R.ok(prodGroupReferenceService.updateById(req));
    }

    /**
     * 新增产品分组引用
     * @param req 产品分组引用
     * @return R
     */
	@ApiOperationSupport(order = 25)
    @Operation(summary = "批量新增产品分组引用", description = "批量新增产品分组引用")
    @SysLog("批量新增产品分组引用")
    @PostMapping("/batchAdd")
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_add')")
    public R save(@RequestBody ProdGroup req) {
		boolean res = true;
		if(ObjectUtil.isNotNull(req.getId()) && ObjectUtil.isNotEmpty(req.getProdGroupReferenceList())) {
			res = prodGroupReferenceService.saveBatch(req.getProdGroupReferenceList());
		}
		prodGroupService.updateProdCount(req.getId());
        return R.ok(res);
    }

    /**
     * 通过id删除产品分组引用
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除产品分组引用", description = "通过id删除产品分组引用")
    @SysLog("通过id删除产品分组引用")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_del')")
    public R remove(@RequestBody Long[] ids) {
		Long groupId = null;
		if(ObjectUtil.isNotEmpty(ids)) {
			ProdGroupReference prodGroupReference = prodGroupReferenceService.getById(ids[0]);
			if(ObjectUtil.isNotNull(prodGroupReference))
				groupId = prodGroupReference.getGroupId();
		}
		boolean res = prodGroupReferenceService.removeBatchByIds(CollUtil.toList(ids));
		prodGroupService.updateProdCount(groupId);
        return R.ok(res);
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_ProdGroup_export')")
    public List<ProdGroupReference> export(ProdGroupReference req, Long[] ids) {
        return prodGroupReferenceService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), ProdGroupReference::getId, ids));
    }
}

